﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Transactions;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Migrations;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    /// <summary>
    /// 1. 诺亚帝可以作为一个机构
    /// 2. 诺亚帝机构的管理员
    /// 3. DeviceTypes
    /// 4. ProductTypes
    /// </summary>
    [Route("api/project")]
    [ApiController]
    public class ProjectController : ControllerBase
    {
        private readonly InstitutionService _institutionService;
        private readonly UserService _userService;
        private readonly UserRoleService _userRoleService;
        private readonly RoleService _roleService;
        private readonly IConfiguration _configuration;
        public ProjectController(InstitutionService institutionService, SMSVerificationCodeService smsVerificationCodeService
            , UserService userService, UserRoleService userRoleService, 
            RoleService roleService,
            IConfiguration configuration)
        {
            _institutionService = institutionService;
            _userService = userService;
            _userRoleService = userRoleService;
            _roleService = roleService;
            _configuration = configuration;
        }        

        /// <summary>
        /// 创建机构信息
        /// </summary>
        /// <param name="institution"></param>
        /// <returns></returns>
        [HttpPost("CreateRoyaltyInstitution")]
        public async Task<ActionResult<bool>> CreateRoyaltyInstitution()
        {
            var royalty = new InstitutionDTO
            {
                Address = "",
                Email = "seiwunsei@hotmail.com",
                Name = "Royalty",
                Info = "诺亚地科技"
            };
            var institutionResult = await _institutionService.CreateInstitutionAsync(royalty);
            var adminRole = await _roleService.GetRoleByIdAsync(1);
            var admin = new UserDTO
            {
                UserType = "0",
                Username = "admin",
                Email = "seiwunsei@hotmail.com",
                Institution = institutionResult,
            };
            admin.Roles = new[]{
                adminRole
            };
            _userService.CreateUserAsync(admin, "1");
            return true;
        }

        [HttpGet("Config")]
        public IActionResult GetConfiguration(string configPath)
        {
            var configValue = _configuration.GetValue<string>(configPath);
            return Ok(configValue);
        }
    }
}